/**
 * 通用工具类
 */
;
(function(global, factory) {

	"use strict";

	if(typeof define === "function" && define.amd) {
		define( ["jquery", "layer"], function(a, b) {
			return factory(global, a, b);
		} );
	} else {
		factory(global);
	}
})(typeof window !== "undefined" ? window : this, function(window, $, layer) {
	
	let _001_getClass = (o) => { //判断数据类型
		return Object.prototype.toString.call(o).slice(8, -1);
	}

	let _002_deepCopy = (target, sources) => {
		Object.assign(target, sources);
	}
	
	let _002_deepCopy__OUTOFDATE = (source, obj) => {
		var result, oClass = _001_getClass(obj);
		for(var i in obj) {
			var copy = obj[i];
			if(_001_getClass(copy) == "Object") {
				if(typeof source[i] !== "object") {
					source[i] = {};
				}
				_002_deepCopy(source[i], copy);
			} 
			else if(_001_getClass(copy) == "Array") {
				if(typeof source[i] !== "object") {
					source[i] = [];
				}
				_002_deepCopy(source[i], copy);
			}
			else source[i] = copy; //基本数据类型则赋值给属性
		}
	}
	
	let _003_textTurnDOM = (plainText) => {
		if(typeof plainText === 'string') {
			let div = document.createElement('div');
			div.innerHTML = plainText;
			let childrenDom = div.children;
			if(childrenDom.length === 1) {
				return childrenDom[0];
			}
			return childrenDom;
		}
		return plainText;
	}
	
	let _004_get_parameter_from_url = (param_name) => {
		var url = window.location.href;
		var params = {};
		if(url && url.indexOf("?")>0){
			var param_str = url.substring(url.lastIndexOf("?")+1);
			if(param_str){
				var param_list = param_str.split("&");
				if(param_list && param_list.length>0){
					for(var i=0; i<param_list.length; i++){
						var tmp_key = param_list[i].substring(0,param_list[i].indexOf("="));
						var tmp_value = param_list[i].substring(1+param_list[i].indexOf("="));
						if(param_name && tmp_key===param_name){
							return tmp_value;
						}
						params[tmp_key] = tmp_value;
					}
				}
			}
		}
		if(param_name){
			//没取到指定的参数
			return;
		}
		return params;
	}
	
	/**
	 * 模态框组件封装
	 */
	let _005_Modal = {
		"_appendHtml": function( title ) {
			let html = '';
			
			html += '<div class="modal fade" id="bootstrapModelWin" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" data-backdrop="false">';
			html += ' <div class="modal-dialog" role="document">';
			html += '  <div class="modal-content">';
			html += '   <div class="modal-header">';
			html += '    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
			html += '    <h4 class="modal-title" id="exampleModalLabel">' + title + '</h4>';
			html += '   </div>';
			html += '   <div class="modal-body">';
			html += '<form></form>';
			html += '   </div>';
			html += '   <div class="modal-footer">';
			html += '    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>';
			html += '    <button type="button" class="btn btn-primary btn-save">保存</button>';
			html += '   </div>';
			html += '  </div>';
			html += ' </div>';
			html += '</div>';
			
			$("body").append(html);
			return $("#bootstrapModelWin");
		},
		/**
		 * 打开模态框
		 * bootstrap
		 */
		"open": function(contentHtml, title) {
			title = title ?? "";
			let modelWin = $("#bootstrapModelWin");
			if(!modelWin.length) {
				modelWin = this._appendHtml(title);
			}
			modelWin.find("form").html(contentHtml);
			modelWin.modal();
			return modelWin;
		},
		/**
		 * 弹出警告框
		 * layer
		 */
		"info": function(msg) {
			layer.alert(msg, {"title": "", "btn":"","icon": 1, time: 2000,offset: '100px'});
		},
		"warn": function(msg) {
			layer.alert(msg, {"icon": 0});
		},
		"error": function(msg) {
			layer.alert(msg, {"icon": 2});
		},
		"toast": function(msg) {
			layer.alert(msg, {"title": "", "shade": false, "btn":"", time: 2000,offset: '100px'});
		}
	}
	
	
	let _006_generateDomId = (dom)=> {
		if(dom && dom.attr("id")) {
			return dom.attr("id");
		}
		let tmpId = 10001, id;
		do {
			id = "auto-generate-" + tmpId++;
		} while($("#"+id).length);
		if(dom) {
			dom.attr("id", id);
		}
		return id;
	}
	
	/**
	 * 遮罩
	 */
	let _007_Mask = {
		"mask": function() {
			if($(".xt-loading").length) {
				return ;
			}
			let html = '<div class="xt-loading"><div></div></div>';
			$("body").append(html);
		},
		"unmask": function() {
			$(".xt-loading").remove();
		}
	}
	


	let commonUtil = {};
	
	//对象拷贝
	commonUtil.deepCopy = _002_deepCopy;
	
	/**
	 * 文本转DOM
	 * 如果文本只有1个DOM，返回该DOM对象
	 * 如果文本包含多个DOM，返回DOM对象数组
	 */
	commonUtil.textTurnDOM = _003_textTurnDOM;
	
	/**
	 * 从url中获取参数
	 */
	commonUtil.getUrlParam = _004_get_parameter_from_url;
	
	/**
	 * 弹出模态框
	 */
	commonUtil.modal = _005_Modal;
	for(let k in _005_Modal) {
		if(k.indexOf('_') !== 0) {
			commonUtil[k] = _005_Modal[k];
		}
	}
	/**
	 * 生成元素id
	 */
	commonUtil.domId = _006_generateDomId;
	
	/**
	 * 遮罩
	 */
	commonUtil.mask = _007_Mask.mask;
	commonUtil.unmask = _007_Mask.unmask;
	
	window.commonUtil = window.$$ = commonUtil;
	
	return commonUtil;
});